/*
 * jfCommunicationSettings.java
 *
 * Created on 11 mai 2008, 16:43
 */
package bbos.Match.Automat.BeforeMatch;

import bbos.General.Views.*;
import bbos.*;
import bbos.General.Model.mPlayerType;
import bbos.General.Model.mTeamRoster;
import bbos.General.Views.Rosters.jtcRenderer;
import bbos.Match.Model.dPlayer;
import bbos.Match.Model.dTeam;
import bbos.Match.Model.rmiTeam;
import javax.swing.*;
import java.awt.*;
import java.rmi.RemoteException;
import java.util.Vector;
import javax.swing.table.TableColumn;

/**
 *
 * @author  frederic
 */
public class jdgChooseStarPlayer extends JDialog {

    long _money;
    rmiTeam _model;
    Vector _starPlayers;
    Vector _availableStars;

    boolean _isChallenger;
            
    /** Creates new form jfCommunicationSettings */
    public jdgChooseStarPlayer(rmiTeam model, long money, Vector starPlayers, boolean isChallenger) {
        initComponents();

        _isChallenger=isChallenger;
        
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        DisplayMode dmode = gs.getDisplayMode();

        if (dmode != null) {
            int screenWidth = dmode.getWidth();
            int screenHeight = dmode.getHeight();
            this.setLocation((screenWidth - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);
        }

        _money = money;
        _model = model;
        _starPlayers = starPlayers;

        _availableStars = new Vector();
        try {
            mTeamRoster roster = mBBoS.getSingleton().getTeamType(_model.getRaceId());
            for (int i = 0; i < roster.getStarPlayersNumber(); i++) {
                mPlayerType player = roster.getStarPlayer(i);
                if (player.getCost() <= _money) {
                    _availableStars.add(player);
                }
            }
        } catch (RemoteException e) {
            System.err.println(e.getMessage());
        }

        for (int i = 0; i < _availableStars.size(); i++) {
            mPlayerType player = (mPlayerType) _availableStars.get(i);
            for (int j = 0; j < _starPlayers.size(); j++) {
                dPlayer playerToTest = (dPlayer) _starPlayers.get(j);
                if (playerToTest.getName().equals(player.getName())) {
                    _availableStars.remove(player);
                    break;
                }
            }
        }

        jtmAvailableStars tableModel = new jtmAvailableStars(_availableStars);

        jtbStarPlayers.setModel(tableModel);
        jtbStarPlayers.setDefaultRenderer(String.class, new jtcRenderer());
        jtbStarPlayers.setDefaultRenderer(Integer.class, new jtcRenderer());

        TableColumn column = null;
        for (int i = 0; i < jtbStarPlayers.getModel().getColumnCount(); i++) {
            column = jtbStarPlayers.getColumnModel().getColumn(i);
            switch (i) {
                case 0:
                    column.setPreferredWidth(100);
                    break;
                case 1:
                    column.setPreferredWidth(15);
                    break;
                case 2:
                    column.setPreferredWidth(15);
                    break;
                case 3:
                    column.setPreferredWidth(15);
                    break;
                case 4:
                    column.setPreferredWidth(15);
                    break;
                case 5:
                    column.setPreferredWidth(450);
                    break;
                case 6:
                    column.setPreferredWidth(40);
                    break;
            }
        }

        repaint();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbStarPlayers = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jbtCancel = new javax.swing.JButton();
        jbtOK = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Treasury to Petty Cash");
        setMinimumSize(new java.awt.Dimension(200, 100));
        setModal(true);
        setResizable(false);

        jPanel1.setPreferredSize(new java.awt.Dimension(800, 200));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jtbStarPlayers.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtbStarPlayers);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setPreferredSize(new java.awt.Dimension(800, 33));

        jbtCancel.setText("Cancel");
        jbtCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCancelActionPerformed(evt);
            }
        });
        jPanel2.add(jbtCancel);

        jbtOK.setText("OK");
        jbtOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtOKActionPerformed(evt);
            }
        });
        jPanel2.add(jbtOK);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        jPanel3.setPreferredSize(new java.awt.Dimension(800, 33));

        jLabel1.setText("Select a star player :");
        jPanel3.add(jLabel1);

        getContentPane().add(jPanel3, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jbtCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCancelActionPerformed
        this.setVisible(false);
}//GEN-LAST:event_jbtCancelActionPerformed

    private void jbtOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtOKActionPerformed
        int index = jtbStarPlayers.getSelectedRow();
        if (index > -1) {
            try
            {
            dPlayer player = new dPlayer((mPlayerType) _availableStars.get(index),_isChallenger, _model.getRace(),null);
            _starPlayers.add(player);
            //_model.addPlayer((mPlayerType) _availableStars.get(index));
            }
            catch (RemoteException e)
            {
                e.printStackTrace();
            }
                    
            this.setVisible(false);
        }
}//GEN-LAST:event_jbtOKActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtOK;
    private javax.swing.JTable jtbStarPlayers;
    // End of variables declaration//GEN-END:variables
    @Override
    public void paint(Graphics g) {
        super.paint(g);

    }
}

